﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Kaidou.Util;
using System.Text.RegularExpressions;

public partial class seal_seal_order_search_kumi : System.Web.UI.Page
{
    private int pageSize = int.Parse(Resources.Resource.pagenumber);

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            LoginModel loginModel = (LoginModel)Session["loginModel"];

            if (loginModel == null)
            {
                Response.Redirect("../login.aspx");
            }
            else if (!loginModel.isKumiaiin())
            {
                Response.Redirect("../menu.aspx");
            }

            if (loginModel.isKumiaiin() && sessioncontrol.control())
            {
                Response.Redirect("../control.aspx");
            }

            if (loginModel.isHonten())
            {
                if (!IsPostBack)
                {
                    MST_Union_BranchPersister dao = new MST_Union_BranchPersister();
                    ArrayList allbranch = dao.getByUnionCode(loginModel.Unioncode);
                    ListItem item = new ListItem();
                    item.Value = 0 + "";
                    item.Text = "000 本店";
                    item.Selected = true;
                    BRANCH_CODE.Items.Insert(1, item);

                    for (int i = 0; i < allbranch.Count; i++)
                    {
                        BranchSearchModel bsm = (BranchSearchModel)allbranch[i];
                        item = new ListItem();
                        item.Value = bsm.Branch_code.ToString();
                        item.Text =String.Format("{0:D3} ", bsm.Branch_code) + " " + bsm.Branch_name;
                        BRANCH_CODE.Items.Insert(i + 2, item);
                    }

                   
                }
            }
            else
            {
                BRANCH_CODE_P.Visible = false;
            }

            

            if (!IsPostBack)
            {
                table.Visible = false;
                none.Visible = false;
                JUCHU_STR_DATE.Text = System.DateTime.Now.ToString("yyyyMMdd");
                JUCHU_END_DATE.Text = System.DateTime.Now.ToString("yyyyMMdd");
            }

            if (Session["back"] != null && (int.Parse(Session["back"].ToString()) == 1))
            {

                OrderSearchModel M = (OrderSearchModel)Session["searchOrderModel"];
                if (M.getJuchu_str_date() != null)
                {
                    JUCHU_STR_DATE.Text = M.getJuchu_str_date();
                }

                if (M.getJuchu_end_date() != null)
                {
                    JUCHU_END_DATE.Text = M.getJuchu_end_date();
                }

                if (M.getJuchu_no() != null)
                {
                    TRN_JUCHU_NO.Text = M.getJuchu_no();
                }
                if (M.getBranch_code() > -1)
                {
                    BRANCH_CODE.Text = M.getBranch_code().ToString();
                }


                page(int.Parse(Session["pageNo"].ToString()), true);

                Session.Remove("back");
            }


        }catch(Exception mee){
            LogManager.error("seal_seal_order_search_kumi fail "+mee.Message);
        }
    }

    private void page(int no, bool back)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];
        //カプセル化オブジェクト
        OrderSearchModel M = new OrderSearchModel();
        if (!back)
        {
            M.setUnion_code( loginModel.Unioncode );
            M.setJuchu_str_date( JUCHU_STR_DATE.Text.Trim().Length==8 ? JUCHU_STR_DATE.Text.Trim() :null);
            M.setJuchu_end_date( JUCHU_END_DATE.Text.Trim().Length==8 ? JUCHU_END_DATE.Text.Trim() :null);
            M.setJuchu_no( TRN_JUCHU_NO.Text.Trim().Length>0? TRN_JUCHU_NO.Text.Trim():null);
            if (loginModel.isHonten())
            {
                M.setBranch_code(int.Parse(BRANCH_CODE.Text));
            }
            else
            {
                M.setBranch_code(loginModel.Branchcode);
            }

            //------------------------------------------validation--------------------------------------
            InputErrorException ie = new InputErrorException(Resources.Resource.ResourceManager);
            if (!StringUtil.isEmpty(JUCHU_STR_DATE.Text))
            {
                if (!StringUtil.isNumber(JUCHU_STR_DATE.Text))
                {
                    ie.addErrorCode("E9001");
                }

                if (JUCHU_STR_DATE.Text.Length < 8)
                {
                    ie.addErrorCode("E9001");
                }
            }

            if (!StringUtil.isEmpty(JUCHU_END_DATE.Text))
            {
                if (!StringUtil.isNumber(JUCHU_END_DATE.Text))
                {
                    ie.addErrorCode("E9001");
                }

                if (JUCHU_END_DATE.Text.Length < 8)
                {

                    ie.addErrorCode("E9001");
                }
            }

            if (M.getJuchu_no() != null)
            {
                Regex r = new Regex(@"^[a-zA-Z]{1}\d{9}$");
                if (!r.IsMatch(M.getJuchu_no()))
                {
                    ie.addErrorCode("E9003");
                }

            }
            List<string> errorCodeList = ie.getErrorMessages();
            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

            if (errorCodeList.Count > 0)
            {
                table.Visible = false;
                none.Visible = true;
                return;
            }
            //------------------------------------------validation--------------------------------------
            
		
        }
        else
        {
            M = (OrderSearchModel)Session["searchOrderModel"];

        }

        Session["searchOrderModel"] = M;
        Session["pageNo"] = no;

        TRN_Juchu_HPersister dao = new TRN_Juchu_HPersister();
        Pager pager = dao.search(M, no, pageSize);
        if (no <= 0) no = 1;
        if (no > pager.Pages) no = pager.Pages;
        pager = dao.search(M, no, pageSize);
        pageNo.Value = no.ToString();
        pages.Text = "全" + pager.Total + "件";
        ArrayList list = pager.List;

        if (list.Count == 0)
        {
            table.Visible = false;
            none.Visible = true;
        }
        else
        {
            none.Visible = false;
            table.Visible = true;
            //datatable.Rows.Clear();
            if (no == 1)
            {
                prebutton.Visible = false;
            }
            else
            {
                prebutton.Visible = true;
                prebutton.Text = "≪ 前の" + pageSize + "件";

            }

            if (no == pager.Pages)
            {
                nextbutton.Visible = false;

            }
            else
            {
                nextbutton.Visible = true;
                nextbutton.Text = " 次の" + pageSize + "件 ≫";
            }

            MST_Union_BranchPersister unionMgr = new MST_Union_BranchPersister();
            StringBuilder sb = new StringBuilder();
            foreach (object o in list)
            {
                OrderSearchModel mu = (OrderSearchModel)o;
                TableRow row = new TableRow();

                TableCell td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.getJuchu_date().ToString("yyyy/MM/dd");
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.Text = loginModel.Parent.OfficeName;
                row.Cells.Add(td);

                mst_union_branch union_branch = unionMgr.getDetail(mu.getUnion_code(), mu.getBranch_code());
                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.Text = union_branch == null ? "本店" : union_branch.officename;
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.getJuchu_clss_moji();
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.getJuchu_no();
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Right;
                td.Text = mu.getTotal_seal_qty().ToString();
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = "<a href='seal_order_detail_kumi.aspx?juchu_no=" + mu.getJuchu_no() + "'>表示</a>";
                row.Cells.Add(td);

                datatable.Rows.Add(row);
            }

        }
    }

    protected void pre(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchOrderModel");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) - 1, false);
    }

    protected void next(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchOrderModel");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) + 1, false);
    }

    protected void search(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchOrderModel");
        Session.Remove("pageNo");
        page(1, false);

    }
}
